#!/bin/sh
##----------------------------------------------------------------------
## Continuous integration run script
##----------------------------------------------------------------------
## Copyright (C) 2007-2014 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------

JUNIT_REPORT=test-out.xml
COVERAGE_REPORT=test-coverage.xml
PEP8_VIOLATIONS_REPORT=pep8-violations.xml
TEST_DB=test_noc

error_exit ( ) {
    printf "\033[1;31m$PROGNAME: ${1:-'Unknown error'}\033[0m\n" 1>&2
    echo "Terminating" 1>&2
    exit 1
}

## Jump to NOC
cd `dirname $0`/..
rm -f $JUNIT_REPORT $COVERAGE_REPORT

## Run
echo "Running CI scripts ..."

echo "Checking test database"
psql -l | grep $TEST_DB
if [ $? -ne 0 ]; then
    dropdb $TEST_DB
fi

echo "Installing .conf files and synchronizing contrib"
./scripts/upgrade || error_exit "Failed"

echo "Running tests"
./noc test --no-interactive --junit-xml-out=$JUNIT_REPORT\
    --coverage-xml-out=$COVERAGE_REPORT

echo "Running violations tests"
./scripts/check_pep8 > $PEP8_VIOLATIONS_REPORT

echo "done"
